import os
import shutil
import glob


def delete_dirs_with_extension(root_dir, extension):
    # 获取根目录下的所有文件夹和文件
    for root, dirs, files in os.walk(root_dir):
        for dir_name in dirs:
            # 检查文件夹名称是否以指定的扩展名结尾
            if dir_name.endswith(extension):
                dir_path = os.path.join(root, dir_name)
                # 删除文件夹及其内容
                try:
                    shutil.rmtree(dir_path)
                    print(f"Deleted directory: {dir_path}")
                except OSError as e:
                    print(f"Error deleting directory: {dir_path} - {e}")

# 指定根目录和文件夹扩展名
folder_extension = '.dir'


def delete_files_with_extensions(directory, extensions):
    for root, dirs, files in os.walk(directory):
        for file_name in files:
            if any(file_name.endswith(ext) for ext in extensions):
                file_path = os.path.join(root, file_name)
                try:
                    os.remove(file_path)
                    print(f"Deleted: {file_path}")
                except Exception as e:
                    print(f"Error deleting {file_path}: {e}")


# 如果需要删除特定目录及其子目录下的文件，可以指定该目录路径
# delete_files_with_extensions('/path/to/your/directory', ['.pbi', '.xcl'])



# 使用方法
if __name__ == "__main__":
    directory_to_search = "."  # 当前目录
    # 调用函数删除文件夹
    delete_dirs_with_extension(directory_to_search, folder_extension)
    # 使用示例：删除当前目录下所有 .pbi 和 .xcl 文件
    delete_files_with_extensions(directory_to_search, ['.pbi', '.xcl', '.dir', '.pbd', '.browse', '.i', '.o', '.cout', '.ninja_deps', '.ninja_log', '.ninja'])
    os.system("pause")
